// pages/addressList/addressList.js
import Http from '../../utils/http.js';
let app = getApp();
var prevPage;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addrList:[],
    currentBizid:'',//当前勾选的地址id
    canAddrChoose:'1',//地址是否可选择
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      ...(options)
    })
    this.getAddrList()
  },
  //修改地址
  handleAddrEdit(e){
    const attrInfo = JSON.stringify(e.detail)
    wx.navigateTo({
      url: `./editAddress/index?attrInfo=${attrInfo}&type=edit&canAddrChoose=${this.data.canAddrChoose}`,
    })
  },
  handleAddrAdd(e) {
    const attrInfo = JSON.stringify(new Object)
    wx.navigateTo({
      url: `./editAddress/index?attrInfo=${attrInfo}&type=add`,
    })
  },
  //获取收货地址
  getAddrList(){
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    Http.Request('GET',`/api/customer/receiveAddress/receiveAddress/v1`).then(res=>{
      this.setData({
        addrList:res
      })
      wx.hideLoading();
    }).catch(err=>{
      wx.hideLoading();
      wx.showToast({
        title: err,
        icon: "none",
        duration: app.globalData.duration
      })
    })
  },
  //获取微信地址
  getWechatAddr(){
    const _this = this
    wx.getSetting({
      success(res) {
        if (res.authSetting['scope.address']) {
          wx.chooseAddress({
            success(res) {
              //如果微信选择的地址在地址列表中不存在，则新增该地址
              if(!_this.isAddrExist(res)){
                wx.showLoading({
                  title: '加载中',
                  mask: true
                })
                const data = {
                  "city": res.cityName,
                  "district": res.countyName,
                  "province": res.provinceName,
                  "receiveAddress": res.detailInfo,
                  "receiveName": res.userName,
                  "receivePhone": res.telNumber
                }
                Http.Request('POST',`/api/customer/receiveAddress/receiveAddress/NOT_DEFAULT/v1`,data).then(res=>{
                  wx.hideLoading();
                }).catch(err=>{
                  wx.hideLoading();
                  wx.showToast({
                    title: err,
                    icon: "none",
                    duration: app.globalData.duration
                  })
                })
              }
            }
          })
        } else {
          if (res.authSetting['scope.address'] == false) {
            wx.openSetting({
              success(res) {

              }
            })
          } else {
            wx.chooseAddress({
              success(res) {
                console.log(res)
              }
            })
          }
        }
      }
    })
  },
  //判断选中的微信地址是否在地址列表中，没有则新增
  isAddrExist(addrObj){
    const addrList = this.data.addrList;
    const isExist = addrList.some(o=>{
      return o.receiveName === addrObj.userName && o.receivePhone === addrObj.telNumber && o.province === addrObj.provinceName && o.city === addrObj.cityName && o.district === addrObj.countyName && o.receiveAddress === addrObj.detailInfo
    })
    return isExist
  },
  //选中地址
  handleAddrSelect(e){
    if (this.data.canAddrChoose=='0')return;
    const detail = e.detail
    this.setData({
      currentBizid:detail.bizid
    })
    if(this.data.refer==='shiwu'){
      prevPage.setData({
        currentAddress: detail.province + detail.city + detail.district + detail.receiveAddress,
        receiveName: detail.receiveName,
        receivePhone: detail.receivePhone,
        addrBizid: detail.bizid
      })
      wx.navigateBack({
        delta: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var pages = getCurrentPages();
    prevPage = pages[pages.length - 2];
  },
  onShareAppMessage() {
    app.globalFn.onShareAppMessage()
  }
})